/*
    每一秒向文件中写入一个系统时间
    年 月 日 时 分 秒
*/
#include <head.h>

//int main(int argc,const char *argv[])
//{
//    while(1)
//    {
//        int fd = open("time.txt",O_RDWR | O_CREAT,0666);
//        FOPEN_ERR(fd);
//        time_t current_time;
//        time(&current_time);
//
//        struct tm *local_time = localtime(&current_time);
//        char buf[1024];
//        strftime(buf,sizeof(buf),"%Y-%m-%d %H:%M:%S",local_time);
//        //lseek(fd,0,SEEK_SET);
//        write(fd,buf,strlen(buf));
//        close(fd);
//        sleep(1);
//    }
//
//    return 0;
//}

void time_file(const char *file_name)
{
    int fd = open(file_name,O_RDWR | O_CREAT,0666);
    //FOPEN_ERR(fd);

    lseek(fd,0,SEEK_SET);
    char buf[1024];
    time_t now = 0;
    time(&now);
    time_t t1 = time(NULL);

    struct tm *time_1 = localtime(&now);

    while(1)
    {
        t1 = time(NULL);
        if(now != t1)
        {
            time_1 = localtime(&now);
            sprintf(buf,"当前时间 %.4d-%.2d-%.2d %.2d:%.2d:%.2d\n",
            time_1->tm_year+1900,time_1->tm_mon+1,time_1->tm_mday,
            time_1->tm_hour,time_1->tm_min,time_1->tm_sec);

            write(fd,buf,strlen(buf));
            now = t1;
        }
    }
    close(fd);

}

int main(int argc ,char const *argv[])
{
    time_file(argv[1]);
    return 0;
}